// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.erosion.categories;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import zombie.erosion.ErosionData;
import zombie.erosion.ErosionMain;
import zombie.erosion.obj.ErosionObj;
import zombie.erosion.obj.ErosionObjSprites;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoObject;
import zombie.iso.sprite.IsoSprite;

public final class Flowerbed extends ErosionCategory {
    private final int[] tileID = new int[]{16, 17, 18, 19, 20, 21, 22, 23, 28, 29, 30, 31};
    private final ArrayList<ErosionObj> objs = new ArrayList();

    @Override
    public boolean replaceExistingObject(
        IsoGridSquare _sq, ErosionData.Square _sqErosionData, ErosionData.Chunk _chunkData, boolean _isExterior, boolean _hasWall
    ) {
        int int0 = _sq.getObjects().size();

        for (int int1 = int0 - 1; int1 >= 0; int1--) {
            IsoSprite isoSprite = _sq.getObjects().get(int1).getSprite();
            if (isoSprite != null && isoSprite.getName() != null) {
                if (isoSprite.getName().startsWith("f_flowerbed_1")) {
                    int int2 = Integer.parseInt(isoSprite.getName().replace("f_flowerbed_1_", ""));
                    if (int2 <= 23) {
                        if (int2 >= 12) {
                            int2 -= 12;
                        }

                        Flowerbed.CategoryData categoryData0 = (Flowerbed.CategoryData)this.setCatModData(_sqErosionData);
                        categoryData0.hasSpawned = true;
                        categoryData0.gameObj = int2;
                        categoryData0.dispSeason = -1;
                        ErosionObj erosionObj0 = (ErosionObj)this.objs.get(categoryData0.gameObj);
                        _sq.getObjects().get(int1).setName(erosionObj0.name);
                        return true;
                    }
                }

                if (isoSprite.getName().startsWith("vegetation_ornamental_01")) {
                    int int3 = Integer.parseInt(isoSprite.getName().replace("vegetation_ornamental_01_", ""));

                    for (int int4 = 0; int4 < this.tileID.length; int4++) {
                        if (this.tileID[int4] == int3) {
                            Flowerbed.CategoryData categoryData1 = (Flowerbed.CategoryData)this.setCatModData(_sqErosionData);
                            categoryData1.hasSpawned = true;
                            categoryData1.gameObj = int4;
                            categoryData1.dispSeason = -1;
                            ErosionObj erosionObj1 = (ErosionObj)this.objs.get(categoryData1.gameObj);
                            _sq.getObjects().get(int1).setName(erosionObj1.name);
                            return true;
                        }
                    }
                }
            }
        }

        return false;
    }

    @Override
    public boolean validateSpawn(
        IsoGridSquare _sq, ErosionData.Square _sqErosionData, ErosionData.Chunk _chunkData, boolean _isExterior, boolean _hasWall, boolean _isRespawn
    ) {
        return false;
    }

    @Override
    public void update(IsoGridSquare _sq, ErosionData.Square _sqErosionData, ErosionCategory.Data _sqCategoryData, ErosionData.Chunk _chunkData, int _eTick) {
        Flowerbed.CategoryData categoryData = (Flowerbed.CategoryData)_sqCategoryData;
        if (!categoryData.doNothing) {
            if (categoryData.gameObj >= 0 && categoryData.gameObj < this.objs.size()) {
                ErosionObj erosionObj = (ErosionObj)this.objs.get(categoryData.gameObj);
                boolean boolean0 = false;
                byte _byte = 0;
                int _int = ErosionMain.getInstance().getSeasons().getSeason();
                boolean boolean1 = false;
                if (_int == 5) {
                    IsoObject isoObject = erosionObj.getObject(_sq, false);
                    if (isoObject != null) {
                        isoObject.setSprite(ErosionMain.getInstance().getSpriteManager().getSprite("blends_natural_01_64"));
                        isoObject.setName(null);
                    }

                    this.clearCatModData(_sqErosionData);
                } else {
                    this.updateObj(_sqErosionData, _sqCategoryData, _sq, erosionObj, boolean0, _byte, _int, boolean1);
                }
            } else {
                this.clearCatModData(_sqErosionData);
            }
        }
    }

    @Override
    public void init() {
        String string = "vegetation_ornamental_01_";

        for (int _int = 0; _int < this.tileID.length; _int++) {
            ErosionObjSprites erosionObjSprites = new ErosionObjSprites(1, "Flowerbed", false, false, false);
            erosionObjSprites.setBase(0, string + this.tileID[_int], 1);
            erosionObjSprites.setBase(0, string + this.tileID[_int], 2);
            erosionObjSprites.setBase(0, string + (this.tileID[_int] + 16), 4);
            ErosionObj erosionObj = new ErosionObj(erosionObjSprites, 30, 0.0F, 0.0F, false);
            this.objs.add(erosionObj);
        }
    }

    @Override
    protected ErosionCategory.Data allocData() {
        return new Flowerbed.CategoryData();
    }

    @Override
    public void getObjectNames(ArrayList<String> list) {
        for (int _int = 0; _int < this.objs.size(); _int++) {
            if (((ErosionObj)this.objs.get(_int)).name != null && !list.contains(((ErosionObj)this.objs.get(_int)).name)) {
                list.add(((ErosionObj)this.objs.get(_int)).name);
            }
        }
    }

    private static final class CategoryData extends ErosionCategory.Data {
        public int gameObj;

        @Override
        public void save(ByteBuffer byteBuffer) {
            super.save(byteBuffer);
            byteBuffer.put((byte)this.gameObj);
        }

        @Override
        public void load(ByteBuffer byteBuffer, int _int) {
            super.load(byteBuffer, _int);
            this.gameObj = byteBuffer.get();
        }
    }
}
